package com.jessechanrui.template.projectcase.infrastructure.demo.support.http.processor;

import com.burukeyou.uniapi.http.core.channel.HttpApiMethodInvocation;
import com.burukeyou.uniapi.http.core.channel.HttpSender;
import com.burukeyou.uniapi.http.core.request.HttpMetadata;
import com.burukeyou.uniapi.http.core.response.HttpResponse;
import com.burukeyou.uniapi.http.extension.HttpApiProcessor;
import com.jessechanrui.template.projectcase.infrastructure.demo.support.http.annotation.AmtdHttpApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author chenrui
 * @date 2024/11/20
 */
@Component
@Slf4j
public class AmtdHttpApiProcessor implements HttpApiProcessor<AmtdHttpApi> {
    @Override
    public HttpMetadata postBeforeHttpMetadata(HttpMetadata httpMetadata, HttpApiMethodInvocation<AmtdHttpApi> methodInvocation) {
        log.info("(请求发送前)在发送请求之前，对Http请求体后置处理");
        return HttpApiProcessor.super.postBeforeHttpMetadata(httpMetadata, methodInvocation);
    }

    @Override
    public HttpResponse<?> postSendingHttpRequest(HttpSender httpSender, HttpMetadata httpMetadata, HttpApiMethodInvocation<AmtdHttpApi> methodInvocation) {
        log.info("(请求发送时)在Http请求发送时处理");
        return HttpApiProcessor.super.postSendingHttpRequest(httpSender, httpMetadata, methodInvocation);
    }

    @Override
    public String postAfterHttpResponseBodyString(String bodyString, HttpResponse<?> rsp, HttpMetadata httpMetadata, HttpApiMethodInvocation<AmtdHttpApi> methodInvocation) {
        log.info("(请求响应后)对响应body文本字符串进行后置处理");
        log.info("访问地址" + httpMetadata.getHttpUrl().toString());
        return HttpApiProcessor.super.postAfterHttpResponseBodyString(bodyString, rsp, httpMetadata, methodInvocation);
    }

    @Override
    public Object postAfterHttpResponseBodyResult(Object bodyResult, HttpResponse<?> rsp, HttpMetadata httpMetadata, HttpApiMethodInvocation<AmtdHttpApi> methodInvocation) {
        log.info("(请求响应后)对响应body反序列化后的结果进行后置处理");
        return HttpApiProcessor.super.postAfterHttpResponseBodyResult(bodyResult, rsp, httpMetadata, methodInvocation);
    }

    @Override
    public Object postAfterMethodReturnValue(Object methodReturnValue, HttpResponse<?> rsp, HttpMetadata httpMetadata, HttpApiMethodInvocation<AmtdHttpApi> methodInvocation) {
        log.info("(请求响应后)对代理的方法的返回值进行后置处理，类似aop的后置处理");
        return HttpApiProcessor.super.postAfterMethodReturnValue(methodReturnValue, rsp, httpMetadata, methodInvocation);
    }
}
